package com.atguigu.gmall.realtime.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/19 14:23
 */
public class MyRedisUtil {
    
    private static JedisPool pool;
    
    public static Jedis getRedisClient() {
        // 1. 先创建连接池
        if (pool == null) {
            synchronized (MyRedisUtil.class) {
                if (pool == null) {
                    final JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(300);
                    config.setMaxIdle(10);
                    config.setMinIdle(5);
                    
                    config.setBlockWhenExhausted(true);
                    config.setMaxWaitMillis(10 * 1000);
                    config.setTestOnBorrow(true);  // ping
                    config.setTestOnReturn(true);
                    pool = new JedisPool(config, "hadoop162", 6379);
                }
            }
        }
        // 2. 从连接池获取客户端
        return pool.getResource();
    }
}
